<template>
	<view>
		<mvBar :mysNavConfig="mysNavConfig" @onLeftClick="onLeftClick"></mvBar>
		<!-- 消息提示 -->
		<u-toast ref="uToast"></u-toast>
		<view class="zname">
			<view>{{name}}</view>
		</view>
		<view class="uniview">
			<uni-card padding="10px 0">
				<template v-slot:title>
					<view class="title">
						<view class="text">
							{{indexinfo.name}}{{indexinfo.unit==''?'(无)':indexinfo.unit=='ma'?'(%)':'('+indexinfo.unit+')'}}
						</view>
						<view class="switch">
							<selectSwitch :checked_bj_color="indexinfo.enableStatus==1?'#18bc37':'#fa3534'"
								:defaultSwitch="indexinfo.enableStatus==1?true:false" @change="switchchange" />
						</view>
					</view>
				</template>
				<view style="padding: 0px 10px;"
					v-if="indexinfo.code=='XAN'||indexinfo.code=='YAN'||indexinfo.code=='ZAN'||indexinfo.code=='POWER'">
					<u-form labelPosition=" left" :model="indexinfo">
						<view class="inputviews">
							<view class="inputview">
								<u-input type="digit" inputAlign='center' v-model="indexinfo.yj" placeholder="下限值"
									border="bottom">
								</u-input>
							</view>
							<view class="texts">{{'<'}} </view>
							<view class="inpvie">一级报警</view>
							<view class="texts"> {{'<'}}</view>
							<view class="inputview">
								<u-input type="digit" inputAlign='center' v-model="indexinfo.unusual" placeholder="上限值"
									border="bottom">
								</u-input>
							</view>
						</view>
						<view class="inputviews">
							<view class="inputview">
								<u-input type="digit" inputAlign='center' v-model="indexinfo.bj" placeholder="下限值"
									border="bottom">
								</u-input>
							</view>
							<view class="texts">{{'<'}} </view>
							<view class="inpvie">二级报警</view>
							<view class="texts"> {{'<'}}</view>
							<view class="inputview">
								<u-input type="digit" inputAlign='center' v-model="indexinfo.warning" placeholder="上限值"
									border="bottom">
								</u-input>
							</view>
						</view>
						<view class="inputviews">
							<view class="inputview">
								<u-input type="digit" inputAlign='center' v-model="indexinfo.gb" placeholder="下限值"
									border="bottom">
								</u-input>
							</view>
							<view class="texts">{{'<'}} </view>
							<view class="inpvie">三级报警</view>
							<view class="texts"> {{'<'}}</view>
							<view class="inputview">
								<u-input type="digit" inputAlign='center' v-model="indexinfo.highRisk" placeholder="上限值"
									border="bottom">
								</u-input>
							</view>
						</view>
					</u-form>
				</view>
				<view style="padding: 0px 10px;" v-else>
					<u-form labelPosition="left">
						<u-form-item label="一级报警" label-width="75" prop="unusual" borderBottom>
							<u-input type="digit" v-model="indexinfo.unusual" placeholder="请输入一级报警值" border="none">
							</u-input>
						</u-form-item>
						<u-form-item label="二级报警" label-width="75" prop="warning" borderBottom>
							<u-input type="digit" v-model="indexinfo.warning" placeholder="请输入二级报警值" border="none">
							</u-input>
						</u-form-item>
						<u-form-item label="三级报警" label-width="75" prop="highRisk" borderBottom>
							<u-input type="digit" v-model="indexinfo.highRisk" placeholder="请输入三级报警值" border="none">
							</u-input>
						</u-form-item>
					</u-form>
				</view>
			</uni-card>
		</view>
		<view style="width: 90%; margin:10% auto;">
			<u-button shape='circle' type="primary" text="确定" @click="alter"></u-button>
		</view>
	</view>
</template>

<script>
	import selectSwitch from "@/components/xuan-switch/xuan-switch.vue";
	export default {
		components: {
			selectSwitch
		},
		data() {
			return {
				mysNavConfig: {
					/* 占位开启  */
					navPadding: true,
					/* 背景 */
					bgColor: "#2979FF",
					/* 标题 （屏幕中心居中 两边图标中心居中使用slot center1） */
					navTitle: {
						text: "修改阈值",
						color: "#ffffff",
						fontSize: "18px", // px upx rpx
						fontWeight: "500", // 100 - 700
					},
					isCustomFn: true,
					btnType: "type2",
					type2Config: {
						// 左图标
						leftPath: "/static/img/back.png",
						// 右图标
						rightPath: "/static/img/home.png",
						// 圆角
						radius: "40rpx",
					}
				},
				userinfo: '',
				equipmentRealId: '',
				name: '',
				pointRealId: '',
				targetid: '',
				indexinfo: '',
				entrust: '',
				urlrouter: '',
			}
		},
		onLoad(parameter) {
			uni.showLoading({
				title: "加载中...",
				mask: true,
			});
			let parameterdata = JSON.parse(decodeURIComponent(parameter.content));
			this.userinfo = uni.getStorageSync('userinfo');
			this.equipmentRealId = parameterdata.equipmentRealId
			this.name = parameterdata.name
			this.pointRealId = parameterdata.pointRealId
			this.targetid = parameterdata.targetid
			this.entrust = parameterdata.entrust
			uni.request({
				url: getApp().globalData.serverAddress + this.$uri.Uri_lookPointInfo.replace(
					"{equipmentRealId}", this
					.equipmentRealId).replace("{pointRealId}", this
					.pointRealId).replace("{userRealId}", this.userinfo
					.userRealId),
				method: "GET",
				success: (res) => {
					if (res.statusCode !== 200) {
						this.$utils.showToast(this, '服务器错误,请联系技术支持人员!', 'error');
						return
					}
					if (res.data.code == 0) {
						for (let itemle of res.data.data.pointFeatures) {
							if (itemle.pointFeatureRealId == this.targetid) {
								this.indexinfo = itemle
							}
						}
						if (this.indexinfo.code == 'POWER' || this.indexinfo.code == 'ZAN' || this
							.indexinfo.code == 'YAN' || this.indexinfo.code == 'XAN') {
							this.indexinfo.yj = this.indexinfo.min.split('&')[0];
							this.indexinfo.bj = this.indexinfo.min.split('&')[1];
							this.indexinfo.gb = this.indexinfo.min.split('&')[2];
						}
					} else {
						this.$utils.showToast(this, res.data.msg, 'error');
					}
				},
				fail() {
					this.$utils.showToast(this, "获取指标失败！", 'error');
				},
				complete() {
					uni.hideLoading();
				}
			})
		},
		methods: {
			//返回
			onLeftClick() {
				var pointTrend = {
					pointRealId: this.pointRealId,
					equipmentRealId: this.equipmentRealId,
					nametitle: this.name,
					entrust: this.entrust,
					indexid: this.targetid,
				}
				this.$store.commit('alterpointTrend', pointTrend)
				uni.redirectTo({
					url: '/pages/equipment/pointTrend?id=2'
				})
			},
			//启用禁用开关
			switchchange() {
				if (this.indexinfo.enableStatus == 1) {
					this.indexinfo.enableStatus = 2
				} else {
					this.indexinfo.enableStatus = 1
				}
			},
			//修改
			alter() {
				//修改阈值之前需要对用户进行判断是创建者还是授权者
				//当entrust是0的时候表示是创建者为1的时候表示授权者
				if (this.entrust == 0) {
					this.urlrouter = this.$uri.Uri_alterTarget;
				} else {
					this.urlrouter = this.$uri.Uri_entrustalterfeature;
				}
				if (this.indexinfo.unusual === '' || this.indexinfo.warning === '' || this.indexinfo.highRisk === '') {
					this.$utils.showToast(this, this.indexinfo.name + "阈值不能为空！", 'warning');
					return
				}
				if (isNaN(Number(this.indexinfo.unusual)) || isNaN(Number(this.indexinfo.warning)) || isNaN(Number(this
						.indexinfo.highRisk))) {
					this.$utils.showToast(this, "请输入数字！", 'warning');
					return
				}
				if (this.indexinfo.code == 'POWER' || this.indexinfo.code == 'XAN' || this.indexinfo.code == 'YAN' || this
					.indexinfo.code == 'ZAN') {
					if (this.indexinfo.yj === '' || this.indexinfo.bj === '' || this.indexinfo.gb === '') {
						this.$utils.showToast(this, this.indexinfo.name + "阈值不能为空", 'warning');
						return;
					}
					if (isNaN(Number(this.indexinfo.yj)) || isNaN(Number(this.indexinfo.bj)) || isNaN(Number(this.indexinfo
							.gb))) {
						this.$utils.showToast(this, "请输入数字！", 'warning');
						return;
					}
					if (Number(this.indexinfo.yj) > Number(this.indexinfo.unusual) || Number(this.indexinfo.unusual) >
						Number(this.indexinfo.bj) || Number(this.indexinfo
							.bj) > Number(this.indexinfo.warning) || Number(this.indexinfo.warning) > Number(this.indexinfo
							.gb) ||
						Number(this.indexinfo.gb) > Number(this.indexinfo.highRisk)) {
						this.$utils.showToast(this, this.indexinfo.name + "阈值设置错误", 'warning');
						return;
					}
					this.indexinfo.min = this.indexinfo.yj + '&' + this.indexinfo.bj + '&' + this.indexinfo.gb;
				} else {
					if (Number(this.indexinfo.unusual) > Number(this.indexinfo.warning) || Number(this.indexinfo.warning) >
						Number(this.indexinfo.highRisk)) {
						this.$utils.showToast(this, this.indexinfo.name + "阈值设置错误！", 'warning');
						return
					}
				}
				var indexlistarr = []
				indexlistarr.push({
					pointFeatureRealId: this.indexinfo.pointFeatureRealId,
					enableStatus: this.indexinfo.enableStatus,
					min: this.indexinfo.min,
					unusual: this.indexinfo.unusual,
					warning: this.indexinfo.warning,
					highRisk: this.indexinfo.highRisk,
				})
				uni.showLoading({
					title: "修改中...",
					mask: true,
				});
				uni.request({
					url: getApp().globalData.serverAddress + this.urlrouter.replace(
						"{userRealId}", this.userinfo.userRealId).replace(
						"{equipmentRealId}", this.equipmentRealId).replace(
						"{pointRealId}", this.pointRealId),
					data: JSON.stringify(indexlistarr),
					dataType: "json",
					method: "POST",
					success: (res) => {
						if (res.statusCode !== 200) {
							this.$utils.showToast(this, '服务器错误,请联系技术支持人员!', 'error');
							return
						}
						if (res.data.code == 0) {
							var pointTrend = {
								pointRealId: this.pointRealId,
								equipmentRealId: this.equipmentRealId,
								nametitle: this.name,
								entrust: this.entrust,
								indexid: this.targetid,
							}
							this.$store.commit('alterpointTrend', pointTrend);
							this.$utils.showToast(this, '修改成功！', 'success', 2,
								'/pages/equipment/pointTrend?id=2');
						} else {
							this.$utils.showToast(this, res.data.msg, 'error');
						}
					},
					fail() {
						this.$utils.showToast(this, "修改失败！", 'error');
					},
					complete() {
						uni.hideLoading();
					}
				})
			},
		}
	};
</script>

<style lang="scss">
	.text {
		width: 73%;
		display: inline-block;
	}

	.uniview {
		width: 90%;
		margin: 5% auto;
		padding: 0px 10px;
		background-color: #ffffff;
		box-shadow: 0px 0px 3px 1px rgba(212, 214, 220, 1.0);
		border-radius: 15px;
	}

	.title {
		padding: 0px 10px;
		line-height: 46px;
		height: 46px;
		border-bottom: 0.5px solid #e0e1e2;
	}

	.switch {
		width: 27%;
		display: inline-block;
	}

	.zname {
		text-align: center;
		color: #3c9cff;
		margin: 10px auto;
		height: 30px;
		font-size: 20px;
		font-weight: bold;
		line-height: 30px;
		padding-bottom: 5px;
	}

	.inputviews view {
		display: inline-block;
	}

	.inputview {
		width: 30%;
	}

	.inpvie {
		height: 45px;
		line-height: 45px;
		width: 30%;
		text-align: center;
		font-size: 16px;
	}

	.texts {
		font-size: 20px;
	}
</style>